package homework.day02;

import java.io.*;

/**
 * 将当前项目目录下的所有.txt文件复制一份。
 * 复制文件的名字格式:原文件名_cp.txt
 *
 * 例如:
 * 复制当前项目目录下的demo.txt时，复制的文件名为demo_cp.txt
 *
 */
public class Test03 {
    public static void main(String[] args) throws IOException {
        File file = new File(".");
        File[] subs = file.listFiles(f -> f.getName().contains(".txt"));
        for (File sub : subs) {
            String name = sub.getName();
            name = name.substring(0, (sub.getName().length() - 4));
            FileInputStream fis = new FileInputStream(name + ".txt");
            FileOutputStream fos = new FileOutputStream(name + "_cp.txt");
            int d = 0;
            while ((d = fis.read()) != -1) {
                fos.write(d);
            }
            System.out.println("复制完毕!");
            fis.close();
            fos.close();

        }
    }
}